﻿(function() {
	'use strict';
	angular.module('services.workOrderData.developWorks')
		.factory('worksComparedService', worksComparedService);
	function worksComparedService(Restangular){
		return {
			query: query,
			getTimeTypes: getTimeTypes,
			getEchartsInstance:getEchartsInstance,
			getQuarterName : getQuarterName,
			fmtDateTitle : fmtDateTitle,
			splitTime : splitTime
		};
		//请求方法
		function query(params,callback){
			Restangular.all('sas/workOrderData/developWorks/query').post(params).then(callback);
		}
		//周期
		function getTimeTypes(){
			return [
				{timeType : '3', timeTypeName : '月'},
				{timeType : '4', timeTypeName : '季'},
				{timeType : '5', timeTypeName : '年'}
			];
		}
		//图表
		function getEchartsInstance(result,title,filedName,percentage,changeTypeName){
			var filedData = [];
			var seriesData = [];
			angular.forEach(result.data, function(res,index) {
				if (filedData.indexOf(res[filedName]) === -1) {
					filedData.push(res[filedName]);
				}
				seriesData[index] = {
					name:res.filedName,
					value: Number(res.percentage.substring(0,res.percentage.length-1))
				};
			});
			var option = {
				title : {
					text: title,
					x:'center'
				},
				animation: false,
				tooltip : {
					trigger: 'item',
					formatter: '{a} <br/>{b} : {c}%'
				},
		  		series : [{
				   name:changeTypeName,
				   type:'pie',
				   radius : '75%',
				   center: ['50%', '50%'],
				   itemStyle: {
			            normal: {
			                label: {
			                    show: true,
			                    formatter: function(param) {
                                    return param.name + '\n' + param.percent + '%';
                                }
			                }
			            }
			        },
				   data: seriesData
			 }]
			};
			return option;
		}
  //季度
		function getQuarterName(data){
	  var quarterName = '';
	  switch (data) {
	    case '1':
	      quarterName = '第一季度';
	      break;
	    case '2':
	      quarterName = '第二季度';
	      break;
	    case '3':
	      quarterName = '第三季度';
	      break;
	    case '4':
	      quarterName = '第四季度';
	      break;
	  }
	  return quarterName;
	 }
	 //显示标题
  function fmtDateTitle(send,timeType){
    var timeTitle = '';
    var startYear = send.startTime.substring(0, 4)+'年';
    var endYear = send.endTime.substring(0, 4)+'年';
    var startMonth = send.startTime.substring(5, 7)+'月';
    var endMonth = send.endTime.substring(5, 7)+'月';
    var booleanYear = startYear === endYear;
    var booleanMonth = startMonth === endMonth;
    switch(timeType){
      case '5':
        timeTitle  = booleanYear ? startYear : startYear+' 至 '+ endYear;
        break;
      case '4':
        var startQuarterName = getQuarterName(send.startQuarter);
        var endQuarterName = getQuarterName(send.endQuarter);
        if(booleanYear && startQuarterName === endQuarterName){
          timeTitle = startYear+startQuarterName;
        }else {
          timeTitle = startYear + startQuarterName +' 至 ' + endYear + endQuarterName;
        }
        break;
      case '3':
        timeTitle = booleanYear && booleanMonth ? startYear + startMonth : startYear + startMonth + ' 至 ' + endYear + endMonth;
        break;
    }
    return timeTitle;
  }
  //季度标题
  function splitTime(timeString) {
			return {
				year:timeString.substring(0, 4),
				quarter: timeString.substring(4, 5)
			};
		}
 }
})();